package com.github.itdachen.controller;

import com.github.itdachen.entity.ProtoTable;
import com.github.itdachen.framework.context.annotation.CheckApiClient;
import com.github.itdachen.framework.context.annotation.IgnoreResponseAdvice;
import com.github.itdachen.framework.context.exception.BizException;
import com.github.itdachen.framework.core.constants.ClientConstant;
import com.github.itdachen.sdk.dto.DevTableColumnDTO;
import com.github.itdachen.sdk.query.ProtoTableQuery;
import com.github.itdachen.sdk.vo.DevTableColumnVO;
import com.github.itdachen.service.IDevTableInfoService;
import com.github.itdachen.sdk.dto.DevTableInfoDTO;
import com.github.itdachen.sdk.query.DevTableInfoQuery;
import com.github.itdachen.sdk.vo.DevTableInfoVO;
import com.github.itdachen.framework.core.response.ServerResponse;
import com.github.itdachen.framework.core.response.TableData;
import com.github.itdachen.service.IDevTableSourceService;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.swing.table.TableColumn;
import java.util.List;
import java.util.Map;

/**
 * 代码生成业务表
 *
 * @author 王大宸
 * @date 2023-09-23 21:25:55
 */
@Controller
@RequestMapping("/dev/table/info")
@CheckApiClient(title = "代码生成业务表", clientId = ClientConstant.CLIENT_WEB)
public class DevTableInfoController {
    private static final Logger logger = LoggerFactory.getLogger(DevTableInfoController.class);
    private final IDevTableInfoService devTableInfoService;
    private final IDevTableSourceService devTableSourceService;

    public DevTableInfoController(IDevTableInfoService devTableInfoService,
                                  IDevTableSourceService devTableSourceService) {
        this.devTableInfoService = devTableInfoService;
        this.devTableSourceService = devTableSourceService;
    }

    /***
     * 分页
     *
     * @author 王大宸
     * @date 2023-09-23 21:25:55
     * @param params  查询参数
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.framework.core.response.TableData < com.github.itdachen.sdk.vo.DevTableInfoVo>>
     */
    @GetMapping(value = "/page")
    @ResponseBody
    public ServerResponse<TableData<DevTableInfoVO>> findDevTableInfoPage(DevTableInfoQuery params) throws Exception {
        return ServerResponse.ok(devTableInfoService.findDevTableInfoPage(params));
    }


    /***
     * 查询数据库中的表
     *
     * @author 王大宸
     * @date 2023/9/23 22:27
     * @param query query
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.framework.core.response.TableData < com.github.itdachen.entity.ProtoTable>>
     */
    @GetMapping(value = "/proto/list")
    @ResponseBody
    public ServerResponse<TableData<ProtoTable>> findProtoList(ProtoTableQuery query) throws Exception {
        return ServerResponse.ok(devTableSourceService.findProtoList(query));
    }

    /***
     * 导入表
     *
     * @author 王大宸
     * @date 2023/9/23 23:30
     * @param tableNames tableNames
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.sdk.vo.DevTableInfoVO>
     */
    @RequestMapping("/import/tables")
    @ResponseBody
    public ServerResponse<DevTableInfoVO> importTableInfo(@RequestParam(name="tableNames") String tableNames) throws Exception {
        List<ProtoTable> tableInfo = devTableSourceService.findTableInfo(tableNames);
        devTableInfoService.saveImpTableInfo(tableInfo);
        return ServerResponse.ok();
    }


    /***
     * 新增
     *
     * @author 王大宸
     * @date 2023-09-23 21:25:55
     * @param devTableInfoDTO 新增数据
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.sdk.dto.devTableInfo>
     */
    @PostMapping(value = "")
    @ResponseBody
    public ServerResponse<DevTableInfoVO> saveDevTableInfo(@Validated @RequestBody DevTableInfoDTO devTableInfoDTO) throws Exception {
        return ServerResponse.ok(devTableInfoService.saveDevTableInfo(devTableInfoDTO));
    }

    /***
     * 修改
     *
     * @author 王大宸
     * @date 2023-09-23 21:25:55
     * @param devTableInfoDTO 修改数据
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.sdk.dto.devTableInfo>
     */
    @PutMapping(value = "/{id}")
    @ResponseBody
    public ServerResponse<DevTableInfoVO> updateDevTableInfo(@Validated @RequestBody DevTableInfoDTO devTableInfoDTO) throws Exception {
        return ServerResponse.ok(devTableInfoService.updateDevTableInfo(devTableInfoDTO));
    }

    /***
     * 根据id查询
     *
     * @author 王大宸
     * @date 2023-09-23 21:25:55
     * @param id 需要查询数据的id
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.sdk.vo.DevTableInfoVo>
     */
    @GetMapping(value = "/{id}")
    @ResponseBody
    public ServerResponse<DevTableInfoVO> getDevTableInfoById(@PathVariable("id") String id) throws Exception {
        return ServerResponse.ok(devTableInfoService.getDevTableInfoById(id));
    }

    /***
     * 删除
     *
     * @author 王大宸
     * @date 2023-09-23 21:25:55
     * @param id 需要删除数据的id
     * @return com.github.itdachen.framework.core.response.ServerResponse<com.github.itdachen.sdk.vo.DevTableInfoVo>
     */
    @DeleteMapping(value = "/{id}")
    @ResponseBody
    public ServerResponse<DevTableInfoVO> removeDevTableInfo(@PathVariable("id") String id) throws Exception {
        devTableInfoService.removeDevTableInfo(id);
        return ServerResponse.ok();
    }


    /***
     * 生成代码
     *
     * @author 王大宸
     * @date 2023/10/2 15:56
     * @param params params
     * @param response response
     * @return void
     */
    @GetMapping(value = "/code")
    @IgnoreResponseAdvice
    public void code(@RequestParam Map<String, String> params,
                     HttpServletResponse response) throws Exception {
        String ids = params.get("id");
        devTableInfoService.downloadCode(ids.split(","), response);
    }

    /***
     * 预览代码
     *
     * @author 王大宸
     * @date 2023/11/15 16:06
     * @param id id
     * @return com.github.itdachen.framework.core.response.ServerResponse<java.util.Map < java.lang.String, java.lang.String>>
     */
    @GetMapping("/preview/{id}")
    @ResponseBody
    public ServerResponse<Map<String, String>> previewCode(@PathVariable("id") String id) throws Exception {
        return ServerResponse.ok(devTableInfoService.previewCode(id));
    }

    /***
     * 添加字段
     *
     * @author 王大宸
     * @date 2023/11/15 17:16
     * @param devTableColumnDTO devTableColumnDTO
     * @return com.github.itdachen.framework.core.response.ServerResponse<javax.swing.table.TableColumn>
     */
    @PostMapping("/save/column")
    @ResponseBody
    public ServerResponse<DevTableColumnVO> saveTableColumn(@RequestBody DevTableColumnDTO devTableColumnDTO) throws Exception {
        return ServerResponse.ok(devTableInfoService.saveTableColumn(devTableColumnDTO));
    }


}
